const app = getApp();

Page({
  data: {
    // 分类映射
    categoryMap: {
      chef: '家常小炒',
      staple: '时令素菜', 
      stirfry: '蒸煮鲜食',
      stew: '荤素炖菜',
      cold: '凉拌小菜',
      soup: '家常汤羹',
      drink: '小酒饮料'
    },
    userAvatar: '/images/headshot.jpg',
    // 分类联动核心数据
    currentCategory: 'chef',
    toView: 'chef',
    isScrolling: false,
    scrollTimer: null,
    categoryPositions: [],

    // 其他原有数据
    cart: [],
    cartCount: 0,
    showCart: false,
    showPasswordModal: false,
    password: '',
    errorMsg: '',
    adminPassword: '123456',
    isAdmin: false,

    // 菜品详情
    showDetail: false,
    selectedDish: null,
    quantity: 1,
    showSpicySelection: false,

    // 选项配置
    flavorOptions: [
      { id: 1, name: "原味", selected: true },
      { id: 2, name: "香辣", selected: false }
    ],
    spicyOptions: [
      { id: 1, name: "微辣", level: 1, selected: true },
      { id: 2, name: "中辣", level: 2, selected: false },
      { id: 3, name: "特辣", level: 3, selected: false }
    ],
    avoidOptions: [
      {name:'不吃辣', isSelected: false}, 
      {name:'少辣', isSelected: false},
      {name:'不吃葱姜蒜' ,isSelected: false}, 
      {name:'不吃香菜', isSelected: false},
      {name:'不吃海鲜',isSelected: false},
      {name: '素食', isSelected: false}],
    selectedAvoids: [],
    selectedCount: 0,
    temperatureOptions: [
      { id: 1, name: "热饮", selected: true },
      { id: 2, name: "常温", selected: false },
      { id: 3, name: "少冰", selected: false },
      { id: 4, name: "多冰", selected: false }
    ],
    sugarOptions: [
      { id: 1, name: "标准糖", selected: true },
      { id: 2, name: "少糖", selected: false },
      { id: 3, name: "半糖", selected: false },
      { id: 4, name: "无糖", selected: false }
    ],

    // 搜索相关
    showSearchModal: false,
    searchKeyword: '',
    searchResults: [],
    searchHistory: [],
    hotSearch: ['冬瓜毛豆', '茭白肉丝', '韭黄炒蛋', '酸菜牛肉', '干香焗大黄鱼'],
    
    // 忌口弹窗
    showAvoidModal: false,
    hasShownAvoidModal: false,

    // 核心数据
    dishesData: []
  },
// 在 JS 文件中添加 clearSearch 方法
clearSearch: function() {
    console.log('清空搜索内容');
    
    this.setData({
      searchKeyword: '', // 清空搜索关键词
      searchResults: []  // 清空搜索结果
    });
  },
  
  // 确保已有这些方法
  onSearchInput: function(e) {
    const keyword = e.detail.value.trim();
    this.setData({ 
      searchKeyword: keyword 
    });
    
    if (keyword) {
      this.searchDishes(keyword);
    } else {
      this.setData({ 
        searchResults: [] 
      });
    }
  },
  
  hideSearchModal: function() {
    this.setData({ 
      showSearchModal: false,
      searchKeyword: '',
      searchResults: []
    });
  },
  
  // 其他相关方法保持不变
  searchDishes: function(keyword) {
    const { dishesData, categoryMap } = this.data;
    const lowerKeyword = keyword.toLowerCase();
    const results = dishesData.filter(dish => {
      return dish.name.toLowerCase().includes(lowerKeyword) ||
             dish.ingredients.toLowerCase().includes(lowerKeyword) ||
             categoryMap[dish.category]?.toLowerCase().includes(lowerKeyword);
    });
    this.setData({ searchResults: results });
  },

  onLoad: function () {
    // 清理重复数据
    this.cleanDuplicateData();
    
    const adminPassword = wx.getStorageSync('adminPassword') || '123456';
    const searchHistory = wx.getStorageSync('searchHistory') || [];
    const cart = wx.getStorageSync('cart') || [];
    const isAdmin = app.globalData?.isAdmin || false;
    const cartCount = cart.reduce((total, item) => total + item.quantity, 0);
    
    // 检查是否已经设置过忌口偏好
    const hasShownAvoidModal = wx.getStorageSync('hasShownAvoidModal') || false;

    // 获取用户头像
    this.getUserAvatar();

    this.initDishesData();
    this.loadCustomDishes();
    this.loadAvoidPreference();

    this.setData({
      adminPassword,
      searchHistory,
      isAdmin,
      cart,
      cartCount,
      hasShownAvoidModal
    });

    if (!hasShownAvoidModal) {
      console.log('首次进入，显示忌口选择弹窗');
      setTimeout(() => {
        this.setData({
          showAvoidModal: true
        });
      }, 800);
    }

    if (app && app.globalData) {
      app.globalData.adminPassword = adminPassword;
    }
  },

  onReady: function () {
    // 页面初始化完成后计算位置
    this.calculateCategoryPositions();
  },

  onShow: function () {
    // 每次页面显示时更新忌口设置
    this.loadAvoidPreference();
    
    // 页面显示时重新计算分类位置
    if (!this.data.categoryPositions || this.data.categoryPositions.length === 0) {
      setTimeout(() => {
        this.calculateCategoryPositions();
      }, 300);
    }

    const adminPassword = wx.getStorageSync('adminPassword') || '123456';
    const cart = wx.getStorageSync('cart') || [];
    const cartCount = cart.reduce((total, item) => total + item.quantity, 0);

    if (this.data.adminPassword !== adminPassword) {
      this.setData({ adminPassword });
      if (app && app.globalData) {
        app.globalData.adminPassword = adminPassword;
      }
    }
    this.setData({ cart, cartCount });
  },

  getUserAvatar: function() {
    const userInfo = wx.getStorageSync('userInfo') || {};
    console.log("user:{}",userInfo);
    this.setData({
      userAvatar: userInfo.avatarUrl || ''
    });
  },

  // 清理重复数据
  cleanDuplicateData: function() {
    // 确保只有一个 showAvoidModal
    if (this.data.categoryMap && this.data.categoryMap.showAvoidModal !== undefined) {
      delete this.data.categoryMap.showAvoidModal;
    }
    
    // 清理存储中的重复键
    const avoidKeys = ['selectedAvoid', 'userAvoidPreferences'];
    const keepKey = 'userAvoidPreferences';
    
    avoidKeys.forEach(key => {
      if (key !== keepKey && wx.getStorageSync(key)) {
        wx.removeStorageSync(key);
      }
    });
  },

  // 加载忌口偏好设置
  loadAvoidPreference: function() {
    const savedAvoids = wx.getStorageSync('userAvoidPreferences') || [];
    this.setData({
      selectedAvoids: savedAvoids
    });
  },

  // 保存忌口偏好
  saveAvoidPreferences: function() {
    const selectedAvoids = this.data.avoidOptions
                              .filter(item => item.isSelected)
                              .map(o => o.name);
    wx.setStorageSync('userAvoidPreferences', selectedAvoids);
    
    // 更新全局数据
    if (getApp().globalData) {
      getApp().globalData.userAvoids = selectedAvoids;
    }
  },

  // 1. 点击分类时触发：切换分类并滚动到对应区域
  switchCategory: function(e) {
    const category = e.currentTarget.dataset.category;
    
    this.setData({
      currentCategory: category,
      toView: category
    });
    
    this.setData({ isScrolling: true });
    
    clearTimeout(this.data.scrollTimer);
    this.data.scrollTimer = setTimeout(() => {
      this.setData({ isScrolling: false });
    }, 500);
  },

  // 2. 计算每个分类在滚动区域中的位置
  calculateCategoryPositions: function() {
    const that = this;
    
    setTimeout(() => {
      const query = wx.createSelectorQuery();
      query.selectAll('.dish-group').boundingClientRect();
      
      query.exec(function(res) {
        if (res && res[0]) {
          const scrollViewQuery = wx.createSelectorQuery();
          scrollViewQuery.select('.dishes').boundingClientRect();
          
          scrollViewQuery.exec(function(scrollRes) {
            if (scrollRes && scrollRes[0]) {
              const scrollViewTop = scrollRes[0].top;
              
              const positions = res[0].map(item => {
                return {
                  id: item.id,
                  top: item.top - scrollViewTop
                };
              });
              
              that.setData({ categoryPositions: positions });
            } else {
              that.setDefaultPositions();
            }
          });
        } else {
          that.setDefaultPositions();
        }
      });
    }, 300);
  },

  // 3. 备用方案：计算位置失败时，用预估的默认位置
  setDefaultPositions: function() {
    const categories = ['chef', 'staple', 'stirfry', 'stew', 'cold', 'soup', 'drink'];
    const positions = categories.map((id, index) => {
      return { id: id, top: index * 200 };
    });
    
    this.setData({ categoryPositions: positions });
  },

  // 4. 监听右侧scroll-view的滚动事件：滚动时同步高亮分类
  onScroll: function(e) {
    if (this.data.isScrolling) return;
    
    const scrollTop = e.detail.scrollTop;
    const categoryPositions = this.data.categoryPositions;
    
    if (!categoryPositions || categoryPositions.length === 0) return;
    
    let currentCategory = this.data.currentCategory;
    
    for (let i = categoryPositions.length - 1; i >= 0; i--) {
      if (scrollTop + 50 >= categoryPositions[i].top) {
        currentCategory = categoryPositions[i].id;
        break;
      }
    }
    
    if (this.data.currentCategory !== currentCategory) {
      this.setData({ currentCategory: currentCategory });
    }
  },

  // 忌口选择相关方法
  toggleAvoid: function(e) {
    const item = e.currentTarget.dataset.item;
    // console.log(item);
    let list = this.data.avoidOptions;
    var matchedItem = list.find(listItem => listItem.name === item.name);
    matchedItem.isSelected = !matchedItem.isSelected;
    this.setData({
      avoidOptions: list
    });
    const count = list.filter(item => item.isSelected).length;
    this.setData({
      selectedCount: count
    });
  },

  // 确认选择忌口（用于首次进入的弹窗）
  confirmAvoidSelection: function() {
    if (this.data.selectedCount === 0) {
      wx.showToast({
        title: '请至少选择一项',
        icon: 'none'
      });
      return;
    }
    
    this.saveAvoidPreferences();
    wx.setStorageSync('hasShownAvoidModal', true);
    
    this.setData({
      showAvoidModal: false,
      hasShownAvoidModal: true
    });
    
    wx.showToast({
      title: '忌口偏好已保存',
      icon: 'success',
      duration: 1500
    });
  },

  // 清空所有选择
  clearAvoidSelection: function() {
    const newList = this.data.avoidOptions.map(item => ({
      ...item,
      isSelected: false
    }))
    this.setData({
      avoidOptions: newList,
      selectedCount: 0
    });
  },

  // 跳过选择（用于首次进入的弹窗）
  skipAvoidSelection: function() {
    // 设置默认选择
    this.setData({
      selectedAvoids: []
    });
    this.saveAvoidPreferences();
    
    wx.setStorageSync('hasShownAvoidModal', true);
    this.setData({
      showAvoidModal: false,
      hasShownAvoidModal: true
    });
    
    wx.showToast({
      title: '可以随时在我的页面中修改',
      icon: 'none',
      duration: 1500
    });
  },

  // 隐藏弹窗
  hideAvoidModal: function() {
    this.setData({
      showAvoidModal: false
    });
  },

  showAvoidModalFunc: function() {
    this.setData({
      showAvoidModal: true
    });
  },

  // 其他原有功能方法
  initDishesData: function () {
    const dishesData = [
      { id: 'chef1', name: '蒜香鱼片', imgUrl: '/images/sxyp1.jpg', ingredients: '黑鱼片，泡椒，蒜', category: 'chef' },
      { id: 'chef2', name: '茭白肉丝', imgUrl: '/images/jbrs1.jpg', ingredients: '茭白，肉丝，榨菜，青椒', category: 'chef' },
      { id: 'chef3', name: '韭黄炒蛋', imgUrl: '/images/jhcd1.jpg', ingredients: '鸡蛋，韭黄', category: 'chef' },
      { id: 'chef4', name: '清炒绿豆芽', imgUrl: '/images/qcldy1.jpg', ingredients: '绿豆芽', category: 'chef' },
      { id: 'chef5', name: '清炒牛心菜', imgUrl: '/images/qcnxc1.jpg', ingredients: '牛心菜，大蒜', category: 'chef' },
      { id: 'chef6', name: '丝瓜炒蛋', imgUrl: '/images/sgcd1.jpg', ingredients: '丝瓜，鸡蛋', category: 'chef' },
      { id: 'chef7', name: '水面筋炒鸡腿菇', imgUrl: '/images/smjcjtg1.jpg', ingredients: '青椒，水面筋，杏鲍菇，香油', category: 'chef' },
      { id: 'chef8', name: '洋葱肉丝', imgUrl: '/images/ycrs1.jpg', ingredients: '猪肉丝，洋葱', category: 'chef' },
      { id: 'chef9', name: '韭菜花炒肉丝', imgUrl: '/images/jchcrs1.jpg', ingredients: '韭菜花，梅花肉', category: 'chef' },
      { id: 'staple1', name: '葱油芋艿', imgUrl: '/images/cyyn2.jpg', ingredients: '芋艿，香葱', category: 'staple' },
      { id: 'stirfry1', name: '干香焗大黄鱼', imgUrl: '/images/gxjdhy3.jpg', ingredients: '大黄鱼，葱姜蒜，洋葱，小米椒', category: 'stirfry' },
      { id: 'stirfry2', name: '干蒸罗氏虾', imgUrl: '/images/gzlsx3.jpg', ingredients: '罗氏虾', category: 'stirfry' },
      { id: 'stew1', name: '花生酱牛尾', imgUrl: '/images/hsjnw4.jpg', ingredients: '牛尾/牛T骨，洋葱', category: 'stew' },
      { id: 'stew2', name: '口蘑炒肉', imgUrl: '/images/kmcr4.jpg', ingredients: '口蘑，猪梅花肉', category: 'stew' },
      { id: 'stew3', name: '排骨黄鱼汤', imgUrl: '/images/pghyt4.jpg', ingredients: '黄鱼，软骨肋排，娃娃菜，肉丸，鱼丸，油豆腐', category: 'stew' },
      { id: 'stew4', name: '酸菜牛肉', imgUrl: '/images/scnr4.jpg', ingredients: '酸菜，牛里脊（谷饲）', category: 'stew' },
      { id: 'stew5', name: '红烧羊肉', imgUrl: '/images/hsyr4.jpg', ingredients: '羊肉，八角，香叶，干辣椒，花椒', category: 'stew' },
      { id: 'cold1', name: '凉拌黄瓜', imgUrl: '/images/llhg.jpg', ingredients: '黄瓜，蒜末，生抽，香醋', category: 'cold' },
      { id: 'cold2', name: '凉拌木耳', imgUrl: '/images/llme.jpg', ingredients: '干木耳，香菜，小米辣', category: 'cold' },
      { id: 'soup1', name: '冬瓜毛豆', imgUrl: '/images/dgmd6.jpg', ingredients: '冬瓜，毛豆', category: 'soup' },
      { id: 'drink1', name: '牛奶咖啡', imgUrl: '/images/nnkf7.jpg', ingredients: '牛奶，白砂糖，Moccona咖啡冻干', category: 'drink' }
    ];

    this.setData({ dishesData });
  },

  loadCustomDishes: function () {
    const customDishes = wx.getStorageSync('customDishes') || [];
    const dishesData = [...this.data.dishesData];

    customDishes.forEach(dish => {
      if (!dishesData.find(d => d.id === dish.id)) {
        dishesData.push(dish);
      }
    });

    this.setData({ dishesData });
  },

  // 搜索功能
  showSearchModal: function () {
    this.setData({ showSearchModal: true, searchKeyword: '', searchResults: [] });
  },

  hideSearchModal: function () {
    this.setData({ showSearchModal: false });
  },

  onSearchInput: function (e) {
    const keyword = e.detail.value.trim();
    this.setData({ searchKeyword: keyword });
    keyword ? this.searchDishes(keyword) : this.setData({ searchResults: [] });
  },

  searchDishes: function (keyword) {
    const { dishesData, categoryMap } = this.data;
    const lowerKeyword = keyword.toLowerCase();
    const results = dishesData.filter(dish => {
      return dish.name.toLowerCase().includes(lowerKeyword) ||
             dish.ingredients.toLowerCase().includes(lowerKeyword) ||
             categoryMap[dish.category]?.toLowerCase().includes(lowerKeyword);
    });
    this.setData({ searchResults: results });
  },

  selectSearchResult: function (e) {
    const item = e.currentTarget.dataset.item;
    if (!item) return;

    this.addToSearchHistory(item.name);
    this.openDishDetailByItem(item);
    this.hideSearchModal();
  },

  addToSearchHistory: function (keyword) {
    let { searchHistory } = this.data;
    searchHistory = searchHistory.filter(item => item !== keyword);
    searchHistory.unshift(keyword);
    if (searchHistory.length > 10) {
      searchHistory = searchHistory.slice(0, 10);
    }
    this.setData({ searchHistory });
    wx.setStorageSync('searchHistory', searchHistory);
  },

  selectSearchHistory: function (e) {
    const keyword = e.currentTarget.dataset.keyword;
    this.setData({ searchKeyword: keyword });
    this.searchDishes(keyword);
  },

  clearSearchHistory: function () {
    this.setData({ searchHistory: [] });
    wx.setStorageSync('searchHistory', []);
  },

  selectHotSearch: function (e) {
    const keyword = e.currentTarget.dataset.keyword;
    this.setData({ searchKeyword: keyword });
    this.searchDishes(keyword);
  },

  // 菜品详情功能
  openDishDetailByItem: function (item) {
    this.setData({
      selectedDish: {
        id: item.id,
        name: item.name,
        imgUrl: item.imgUrl,
        ingredients: item.ingredients,
        isDrink: item.category === 'drink'
      },
      showDetail: true,
      quantity: 1,
      flavorOptions: this.data.flavorOptions.map(opt => ({ ...opt, selected: opt.id === 1 })),
      spicyOptions: this.data.spicyOptions.map(opt => ({ ...opt, selected: opt.id === 1 })),
      showSpicySelection: false
    });
  },

  openDishDetail: function (e) {
    const dataset = e.currentTarget.dataset;
    const dish = this.data.dishesData.find(d => d.id === dataset.id) || {};
    
    this.setData({
      selectedDish: {
        id: dataset.id,
        name: dataset.name,
        imgUrl: dataset.img,
        ingredients: dataset.desc || "暂无描述",
        isDrink: dish.category === 'drink'
      },
      showDetail: true,
      quantity: 1,
      flavorOptions: this.data.flavorOptions.map(opt => ({ ...opt, selected: opt.id === 1 })),
      spicyOptions: this.data.spicyOptions.map(opt => ({ ...opt, selected: opt.id === 1 })),
      showSpicySelection: false
    });
  },

  openDrinkDetail: function (e) {
    const dataset = e.currentTarget.dataset;
    this.setData({
      selectedDish: {
        id: dataset.id,
        name: dataset.name,
        imgUrl: dataset.img,
        ingredients: dataset.desc || "暂无描述",
        isDrink: true
      },
      showDetail: true,
      quantity: 1,
      temperatureOptions: this.data.temperatureOptions.map(opt => ({ ...opt, selected: opt.id === 1 })),
      sugarOptions: this.data.sugarOptions.map(opt => ({ ...opt, selected: opt.id === 1 }))
    });
  },

  closeDetail: function () {
    this.setData({ showDetail: false, selectedDish: null });
  },

  // 选项选择
  selectFlavor: function (e) {
    const flavorId = e.currentTarget.dataset.id;
    const flavorOptions = this.data.flavorOptions.map(opt => ({
      ...opt, selected: opt.id === flavorId
    }));
    this.setData({ 
      flavorOptions,
      showSpicySelection: flavorOptions.some(opt => opt.id === 2 && opt.selected)
    });
  },

  selectSpicy: function (e) {
    const spicyId = e.currentTarget.dataset.id;
    const spicyOptions = this.data.spicyOptions.map(opt => ({
      ...opt, selected: opt.id === spicyId
    }));
    this.setData({ spicyOptions });
  },

  selectTemperature: function (e) {
    const tempId = e.currentTarget.dataset.id;
    const temperatureOptions = this.data.temperatureOptions.map(opt => ({
      ...opt, selected: opt.id === tempId
    }));
    this.setData({ temperatureOptions });
  },

  selectSugar: function (e) {
    const sugarId = e.currentTarget.dataset.id;
    const sugarOptions = this.data.sugarOptions.map(opt => ({
      ...opt, selected: opt.id === sugarId
    }));
    this.setData({ sugarOptions });
  },

  // 加入购物车 - 修正版本
  addToCartFromDetail: function () {
    if (!this.data.selectedDish) return;

    const selectedDish = this.data.selectedDish;
    const quantity = this.data.quantity;
    const avoids = this.data.selectedAvoids;
    
    let itemData = {
      id: selectedDish.id,
      name: selectedDish.name,
      imgUrl: selectedDish.imgUrl,
      quantity: quantity,
      isDrink: selectedDish.isDrink,
      ingredients: selectedDish.ingredients,
      avoids: avoids
    };

    if (selectedDish.isDrink) {
      const temp = this.data.temperatureOptions.find(opt => opt.selected);
      const sugar = this.data.sugarOptions.find(opt => opt.selected);
      itemData.temperature = temp?.name || '';
      itemData.sugar = sugar?.name || '';
    } else {
      const flavor = this.data.flavorOptions.find(opt => opt.selected);
      const spicy = this.data.spicyOptions.find(opt => opt.selected);
      itemData.flavor = flavor?.name || '';
      if (flavor?.id === 2) itemData.spicy = spicy?.name || '';
    }

    let cart = [...this.data.cart];
    const index = cart.findIndex(item => item.id === selectedDish.id);
    
    if (index > -1) {
      cart[index].quantity += quantity;
      Object.assign(cart[index], itemData);
    } else {
      cart.push(itemData);
    }

    this.setData({ cart });
    this.updateCartStatus();
    wx.showToast({ title: '已加入收藏清单', icon: 'success' });
    this.closeDetail();
  },

  increaseQuantity: function (e) {
    const id = e.currentTarget.dataset.id;
    let cart = [...this.data.cart];
    const item = cart.find(item => item.id === id);
    if (item) {
      item.quantity++;
      this.setData({ cart });
      this.updateCartStatus();
    }
  },

  decreaseQuantity: function (e) {
    const id = e.currentTarget.dataset.id;
    let cart = [...this.data.cart];
    const index = cart.findIndex(item => item.id === id);
    if (index > -1) {
      if (cart[index].quantity > 1) {
        cart[index].quantity--;
      } else {
        cart.splice(index, 1);
      }
      this.setData({ cart });
      this.updateCartStatus();
    }
  },

  updateCartStatus: function () {
    const cart = this.data.cart;
    const cartCount = cart.reduce((total, item) => total + item.quantity, 0);
    this.setData({ cartCount });
    wx.setStorageSync('cart', cart);
  },

  toggleCart: function () {
    if (this.data.cartCount > 0) {
      this.setData({ showCart: !this.data.showCart });
    }
  },

  hideCart: function () {
    this.setData({ showCart: false });
  },

  clearCart: function () {
    wx.showModal({
      title: '确认清空',
      content: '确定要清空收藏清单吗？',
      success: (res) => {
        if (res.confirm) {
          this.setData({ cart: [] });
          this.updateCartStatus();
          this.hideCart();
        }
      }
    });
  },

  // 订单功能
  showPasswordModal: function () {
    if (this.data.cartCount === 0) {
      wx.showToast({ title: '请先收藏菜品', icon: 'none' });
      return;
    }
    this.setData({ showPasswordModal: true, password: '', errorMsg: '' });
  },

  hidePasswordModal: function () {
    this.setData({ showPasswordModal: false });
  },

  onPasswordInput: function (e) {
    this.setData({ password: e.detail.value });
  },

  confirmCheckout: function () {
    const { password, adminPassword, cart } = this.data;

    if (!password) {
      this.setData({ errorMsg: '请输入密码' });
      return;
    }

    if (password !== adminPassword) {
      this.setData({ errorMsg: '密码错误' });
      return;
    }

    if (cart.length === 0) {
      this.setData({ errorMsg: '收藏清单为空' });
      return;
    }

    const order = {
      id: 'ORD' + new Date().getTime() + Math.floor(Math.random() * 1000),
      items: JSON.parse(JSON.stringify(cart)),
      createTime: new Date().getTime(),
      status: '已完成'
    };

    let orders = wx.getStorageSync('orders') || [];
    orders.unshift(order);
    wx.setStorageSync('orders', orders);

    wx.showToast({ title: '收藏成功', icon: 'success' });

    this.setData({ cart: [] });
    this.updateCartStatus();
    this.hidePasswordModal();

    setTimeout(() => {
      wx.switchTab({ url: '/pages/order/order' });
    }, 1500);
  }
});